﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.ThucThe;

namespace TracNghiemTinHocDaiCuong.DAO
{
    public class ChuDeDAO
    {
        public static ChuDe TimTheoId(int idChuDe)
        {
            string query = string.Format("SELECT * FROM ChuDe WHERE MaCD=@MaCD");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaCD", idChuDe)
            };
            IDataReader read = SqlHelper.ExecuteReader(query, parameter);
            ChuDe chuDe = new ChuDe();
            while (read.Read())
            {
                chuDe = DoiTuong(read);
            }
            return chuDe;
        }

        public static ChuDe DoiTuong(IDataRecord read)
        {
            return new ChuDe
            {
                MaCD = Methods.ConvertNumber(read["MaCD"].ToString()),
                TenCD = Methods.ConvertString(read["TenCD"].ToString()),
                GhiChu = Methods.ConvertString(read["GhiChu"].ToString())
            };
        }

        public static IList<ChuDe> LayDanhSach()
        {
            IList<ChuDe> danhSachChuDe = new List<ChuDe>();
            string query = string.Format("SELECT * FROM ChuDe");
            try
            {
                IDataReader read = SqlHelper.ExecuteReader(query);
                while (read.Read())
                {
                    danhSachChuDe.Add(DoiTuong(read));
                }
                return danhSachChuDe;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        public static bool Them(ChuDe chuDe)
        {
            string query = string.Format("INSERT INTO ChuDe(TenCD, GhiChu) VALUES(@TenCD, @GhiChu)");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@TenCD", chuDe.TenCD),
                new SqlParameter("@GhiChu", chuDe.GhiChu)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Sua(ChuDe chuDe)
        {
            string query = string.Format("UPDATE ChuDe SET TenCD=@TenCD, GhiChu=@GhiChu WHERE MaCD=@MaCD");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@TenCD", chuDe.TenCD),
                new SqlParameter("@GhiChu", chuDe.GhiChu),
                new SqlParameter("@MaCD", chuDe.MaCD)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Xoa(int idChuDe)
        {
            string query = string.Format("DELETE FROM ChuDe WHERE MaCD=@MaCD");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaCD", idChuDe)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }
    }
}